﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;

namespace _1042009_1042034
{
    public partial class uctDanhSachKhachThuePhong : UserControl
    {
        public uctDanhSachKhachThuePhong()
        {
            InitializeComponent();
        }

        List<LoaiKhachHangDTO> ListLoaiKhachHang = Business.LoaiKhachHangService.LayDanhSach();        
        MyDataGridView dgv = new MyDataGridView();


        protected void CreateColumn()
        {
            this.dgv.Columns.Clear();

            DataTable DataLoaiKhachHang = Business.LoaiKhachHangService.ConvertListLoaiKhachHangDTOToDataTable(ListLoaiKhachHang);
                        
            this.dgv.AddTextColumn(true, "STT", "STT");
            //this.dgv.Columns["STT"].AutoIncrementSeed = 1;
                        
            this.dgv.AddTextColumn(true, "MaKhachHang", "Mã Khách");
            this.dgv.AddTextColumn(false, "HoTen", "Họ và Tên");
            this.dgv.AddComboBoxColumns(false, "MaLoaiKhachHang", "Loại Khách", DataLoaiKhachHang, LoaiKhachHangDTO.ValueMember, LoaiKhachHangDTO.DisplayMember);

            this.dgv.AddTextColumn(false, "CMND", "CMND");
            this.dgv.AddTextColumn(false, "DiaChi", "Địa Chỉ");
            this.dgv.AddCheckBoxColumn("Check", "Check");
            
            GetColoumnIndex();
        }
        int iMaKhachHang;
        int iCheck;
        int iMaLoaiKhachHang;
        int iHoTen;
        int iCMND;
        int iDiaChi;
        void GetColoumnIndex()
        {

            iMaKhachHang = this.dgv.GetColumIndexByName("MaKhachHang");
            iHoTen = this.dgv.GetColumIndexByName("HoTen");
            iMaLoaiKhachHang = this.dgv.GetColumIndexByName("MaLoaiKhachHang");
            iCMND = this.dgv.GetColumIndexByName("CMND");
            iDiaChi = this.dgv.GetColumIndexByName("DiaChi");
            iCheck = this.dgv.GetColumIndexByName("Check");

        }

        void SetUpForm()
        {
            this.AutoSize = false;
            this.AutoScroll = false;
            this.Size = Form1.SizeContext;
            panel.Size = this.dgv.Size;
            panel.Controls.Add(this.dgv);
            panel.AutoScroll = false;
            panel.AutoSize = false;
        }
        protected void SetUpDataGridView()
        {
            this.dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            this.dgv.Rows.Clear();
            this.dgv.Columns.Clear();
            this.dgv.Location = new Point(0, 0);
            this.dgv.Size = new Size(Form1.SizeContext.Width - 180, 140);
            this.dgv.ScrollBars = ScrollBars.Both;
            this.dgv.TopLeftHeaderCell.Value = "Select All";
            this.dgv.BackgroundColor = Color.White;
            // dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader; 
            // cho hien dong trang.
            this.dgv.AllowUserToAddRows = true; 
        }

        private void uctDanhSachKhachThuePhong_Load(object sender, EventArgs e)
        {
            this.SetUpDataGridView();
            this.SetUpForm();
            this.CreateColumn();
            this.dgv.Rows.Add(5);
            //su kien
            this.dgv.CellValueChanged += new DataGridViewCellEventHandler(dgv_CellValueChanged);
        }
        

        int iStt = 0;
        int iMaKhachHangIdentity = Business.KhachHangService.GetIdentity();
        

        void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int ColIndex = this.dgv.SelectedCells[0].ColumnIndex;
            int RowIndex = this.dgv.SelectedCells[0].RowIndex;

            if (this.dgv.Rows[RowIndex].Cells[0].Value == null)
            {
                if (iStt == RowIndex)
                {
                    iStt++;
                    this.dgv.Rows[RowIndex].Cells[0].Value = iStt.ToString();
                    this.dgv.Rows[RowIndex].Cells[iMaKhachHang].Value = (this.iStt + this.iMaKhachHangIdentity).ToString();
                }
                else
                {
                    this.dgv[ColIndex, RowIndex].Value = null;
                    return;
                }
            }
            if (ColIndex != 0 )
            {
                this.dgv.SetRowColor(RowIndex, MyColor.Edit);
            }
            if (ColIndex != iCheck && ColIndex != 0)//0 la Stt
            {
                this.dgv.Rows[RowIndex].Cells[iCheck].Value = "Checked";
            }
        }

        void RemoveRow(int iRow)
        {
            this.dgv[this.iCheck, 0].Selected = true;
            for (int i = iRow; i < this.dgv.Rows.Count - 1; i++)
            {
                if (this.dgv.Rows[i + 1].Cells[0].Value != null)
                {
                    this.dgv.Rows[i].Cells[this.iMaLoaiKhachHang].Value = this.dgv.Rows[i + 1].Cells[iMaLoaiKhachHang].Value;
                    this.dgv.Rows[i].Cells[this.iHoTen].Value = this.dgv.Rows[i + 1].Cells[iHoTen].Value;
                    this.dgv.Rows[i].Cells[iCMND].Value = this.dgv.Rows[i + 1].Cells[iCMND].Value;
                    this.dgv.Rows[i].Cells[this.iDiaChi].Value = this.dgv.Rows[i + 1].Cells[iDiaChi].Value;
                    this.dgv.Rows[i].Cells[this.iCheck].Value = this.dgv.Rows[i + 1].Cells[this.iCheck].Value;
                }
                else
                {
                    this.dgv.Rows[i].Cells[0].Value = null;
                    this.dgv.Rows[i].Cells[this.iMaKhachHang].Value = null;
                    this.dgv.Rows[i].Cells[this.iMaLoaiKhachHang].Value = null;
                    this.dgv.Rows[i].Cells[this.iHoTen].Value = null;
                    this.dgv.Rows[i].Cells[iCMND].Value = null;
                    this.dgv.Rows[i].Cells[this.iDiaChi].Value = null;
                    this.dgv.Rows[i].Cells[this.iCheck].Value = null;
                }
            }

            if (this.dgv.Rows.Count > 8)
            {
                this.dgv.Rows.RemoveAt(this.iStt - 1);
            }
            else
            {
                this.dgv.SetRowColor(this.iStt - 1, MyColor.White);
            }
            this.iStt--;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int CheckColIndex = iCheck;
            for (int i = this.dgv.Rows.Count - 1; i >= 0; i--)
            {
                if (this.dgv.Rows[i].Cells[CheckColIndex].Value != null)
                {
                    if (this.dgv.Rows[i].Cells[CheckColIndex].Value.ToString() == "Checked")
                    {
                        //tien hanh xoa du lieu tren form
                        this.RemoveRow(i);
                    }
                }
            }           
        }


        /////////////////////////
        //kiem tra khach trong phong co hop le hay khong


        KhachHangDTO GetDataOfRows(int iRow)
        {
            KhachHangDTO x = new KhachHangDTO();

            if( this.dgv[this.iMaKhachHang, iRow].Value != null)
            {
                x.MaKhachHang = int.Parse(this.dgv[this.iMaKhachHang, iRow].Value.ToString());
            }
            if (this.dgv[this.iMaLoaiKhachHang, iRow].Value != null)
            {
                x.MaLoaiKhachHang = int.Parse(this.dgv[this.iMaLoaiKhachHang, iRow].Value.ToString());
            }


            if (this.dgv[this.iHoTen, iRow].Value != null)
            {
                x.HoTen = this.dgv[this.iHoTen, iRow].Value.ToString();
            }
            if (this.dgv[this.iCMND, iRow].Value != null)
            {
                x.CMND = this.dgv[this.iCMND, iRow].Value.ToString();
            }
            if (this.dgv[iDiaChi, iRow].Value != null)
            {
                x.DiaChi = this.dgv[this.iDiaChi, iRow].Value.ToString();
            }
            
            return x;
        }

        List<KhachHangDTO> lst = new List<KhachHangDTO>();
        public bool KiemTraKhach()
        { 
            //kiem tra tinh hop le cua du lieu
            lst.Clear();
            KhachHangDTO Kh = new KhachHangDTO();
            bool Flag = true;
            for (int i = 0; i < this.dgv.Rows.Count; i++)
            {
                if (this.dgv[this.iCheck, i].Value != null && this.dgv[this.iCheck, i].Value.ToString() == "Checked")
                {
                    Kh = this.GetDataOfRows(i);
                    if (Kh.KiemTra() == false)
                    {
                        Flag = false;
                        this.dgv.SetRowColor(i, MyColor.Eror);
                    }
                    this.lst.Add(Kh);
                }
            }
            if (Flag == false)
                this.lst.Clear();

            return Flag;
        }
        public List<KhachHangDTO> LayDanhSachKhachThue()
        {
            this.KiemTraKhach();
            return this.lst;
        }

    }
}
